import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:flutter/widgets.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/state.dart';

import '../global_store/state.dart';
import '../home_page/camera_component/state.dart';
import '../home_page/mine_component/state.dart';
import 'answer_list_component/state.dart';
import 'homework_list_component/state.dart';
import 'practice_list_component/state.dart';
import 'student_mine_component/state.dart';
import 'subject_tab_component/state.dart';

class HomeState implements GlobalBaseState, Cloneable<HomeState> {
  bool isLoading = false; // 是否正在请求数据中

  // tab页
  List<BottomNavigationBarItem> myTabs = [];
  var tabIndex = 0;
  var tabData = [];

  // list数据
  AnswerListState answerListState = AnswerListState();
  HomeworkListState homeworkListState = HomeworkListState();
  SubjectTabState subjectTabState = SubjectTabState();
  PracticeListState practiceListState = PracticeListState();

  HomeState() {
    if (GlobalState.isTeacher) {
      tabData = [
        {
          'text': '作业管理',
          'title': '作业管理',
          'bodyName': 'homework',
          'icon': new ImageIcon(AssetImage("assets/images/aa.png"))
        },
        {
          'text': '我的写作',
          'title': '我的写作',
          'bodyName': 'practice',
          'icon': new ImageIcon(AssetImage("assets/images/dd.png"))
        },
        {'text': '布置作业', 'title': '', 'bodyName': '', 'icon': ImageIcon(null)},
        {
          'text': '题库',
          'title': '题库',
          'bodyName': 'subject',
          'icon': new ImageIcon(AssetImage("assets/images/cc.png"))
        },
        {
          'text': '我的',
          'title': '我的',
          'bodyName': 'mine',
          'icon': new ImageIcon(AssetImage("assets/images/bb.png"))
        }
      ];
    } else {
      tabData = [
        {
          'text': '批改',
          'title': 'IN课堂中文作文智能批改',
          'bodyName': 'camera',
          'icon': new ImageIcon(AssetImage("assets/images/nav_try.png"))
        },
        {
          'text': '作业',
          'title': '作业中心',
          'bodyName': 'answer',
          'icon': new ImageIcon(AssetImage("assets/images/nav_homework.png"))
        },
        {
          'text': '我的',
          'title': '我的',
          'bodyName': 'student_mine',
          'icon': new ImageIcon(AssetImage("assets/images/nav_mine.png"))
        }
      ];
    }

    myTabs = tabData
        .map<BottomNavigationBarItem>((tab) => BottomNavigationBarItem(
            title: new Text(tab['text']), icon: tab['icon']))
        .toList();
  }

  @override
  HomeState clone() {
    return HomeState()
      ..isLoading = isLoading
      ..userInfo = userInfo
      ..myTabs = myTabs
      ..tabIndex = tabIndex
      ..tabData = tabData
      ..answerListState = answerListState
      ..homeworkListState = homeworkListState
      ..practiceListState = practiceListState
      ..subjectTabState = subjectTabState;
  }

  @override
  Color themeColor;

  @override
  Map<String, dynamic> userInfo;
}

HomeState initState(Map<String, dynamic> args) {
  return HomeState();
}

class CameraConnector extends ConnOp<HomeState, CameraState> {
  @override
  CameraState get(HomeState state) {
    final CameraState cameraState = CameraState();
//    reportState.total = state.toDos.length;
//    reportState.done =
//        state.toDos.where((ToDoState tds) => tds.isDone).toList().length;
    return cameraState;
  }

  @override
  void set(HomeState state, CameraState subState) {}
}

class MineConnector extends ConnOp<HomeState, MineState> {
  @override
  MineState get(HomeState state) {
    return MineState()..userInfo = state.userInfo;
  }

  @override
  void set(HomeState state, MineState subState) {
    state.userInfo = subState.userInfo;
  }
}

class StudentMineConnector extends ConnOp<HomeState, StudentMineState> {
  @override
  StudentMineState get(HomeState state) {
    final StudentMineState mineState = StudentMineState();
    mineState.userInfo = state.userInfo;
    return mineState;
  }

  @override
  void set(HomeState state, StudentMineState subState) {
    state.userInfo = subState.userInfo;
  }
}

class AnswerConnector extends ConnOp<HomeState, AnswerListState> {
  @override
  AnswerListState get(HomeState state) {
    return state.answerListState;
  }

  @override
  void set(HomeState state, AnswerListState subState) {
    state.answerListState = subState;
  }
}

class HomeworkConnector extends ConnOp<HomeState, HomeworkListState> {
  @override
  HomeworkListState get(HomeState state) {
    return state.homeworkListState;
  }

  @override
  void set(HomeState state, HomeworkListState subState) {
    state.homeworkListState = subState;
  }
}

class PracticeConnector extends ConnOp<HomeState, PracticeListState> {
  @override
  PracticeListState get(HomeState state) {
    return state.practiceListState;
  }

  @override
  void set(HomeState state, PracticeListState subState) {
    state.practiceListState = subState;
  }
}

class SubjectConnector extends ConnOp<HomeState, SubjectTabState> {
  @override
  SubjectTabState get(HomeState state) {
    return state.subjectTabState;
  }

  @override
  void set(HomeState state, SubjectTabState subState) {
    state.subjectTabState = subState;
  }
}
